import { loginAPI } from "@/apis/login";
import { defineStore } from "pinia";
import { ref } from "vue";

export interface UserInfo {
	account: string,
	password: string,
	token: string,
}

export const useUserStore = defineStore("user", () => {
	const userInfo = ref<UserInfo | null>(null);
	const code = ref("0");

	async function getUserInfo({ account, password }: { account: string, password: string }) {
		const res: any = await loginAPI({ account, password });
		userInfo.value = res.result;
		code.value = res.code;
		console.log(userInfo.value);
		console.log(res);
	}

	function clearUserInfo() {
		userInfo.value = null;
		code.value = "0";
	}

	return { code, userInfo, getUserInfo, clearUserInfo };
}, {
	persist: true
});